// pages/bindingGuild/President/authenticate/authenticate.js
const app = getApp();
const Dec = require('../../../../utils/public.js');//引用封装好的加密解密
const CusBase64 = require('../../../../utils/base64.js');//引用base64
Page({

  /**
   * 页面的初始数据
   */
  data: {
    p_id: null, //选择平台后返回
    p_name: null,
    p_table_flag: null,

    guild_id:null,
    guild_name:null,

    checkes: false,
    disabled: true,

    text_guild:null,
    text_account:null,
    text_password:null,

    type:null,



  },
  go_protocol: function () {
    wx.navigateTo({
      url: '../../../protocol/protocol',
    })
  },
  go_selec: function() {
    if(this.data.guild_id){
      return false;
    }
    else{
      wx.redirectTo({
        url: '../../platform/platform?process_flag=president_auth',
      })
    }
    
  },
  checkboxChange: function(e) {
    console.log(e.detail.value)
    this.setData({
      checkes: e.detail.value,
    })
  },
  //点击
  click_guild: function() {
    if (this.data.p_id) {
      this.setData({
        disabled: false
      })
    } else {
      wx.showToast({
        icon: 'none',
        title: '请先选择直播平台',
      })
    }
  },
  click_anchor: function() {
    if (this.data.p_id) {
      if (this.data.p_id) {
        this.setData({
          disabled: false
        })
      } else {
        wx.showToast({
          icon: 'none',
          title: '请先选择直播平台',
        })
      }
    }
  },
  click_account: function() {
    if (this.data.p_id) {
      this.setData({
        disabled: false
      })
    } else if (this.data.guild_id){
      this.setData({
        disabled: false
      })
    }
     else {
      wx.showToast({
        icon: 'none',
        title: '请先选择直播平台',
      })
    }
  },
  click_password: function() {
    if (this.data.p_id) {
      this.setData({
        disabled: false
      })
    } else if (this.data.guild_id) {
      this.setData({
        disabled: false
      })
    }
     else {
      wx.showToast({
        icon: 'none',
        title: '请先选择直播平台',
      })
    }
  },
  //输入
  inp_guild:function(e){
    // console.log(e.detail.value)
    this.setData({
      text_guild: e.detail.value,
    })
  },
  inp_account: function (e) {
    // console.log(e.detail.value)
    let aes_account = Dec.Encrypt(e.detail.value);
    this.setData({
      text_account: aes_account,
    })
    // console.log(aes_account)

    
  },
  inp_password: function (e) {
    // console.log(e.detail.value)
    let aes_password = Dec.Encrypt(e.detail.value);
    this.setData({
      text_password: aes_password,
    })
    // console.log('aes+' + aes_password)
  },
  //提交
  click_submit:function(){
    var that = this;
    if (this.data.checkes == "check") {
      // console.log("ok")
      if (this.data.type =="upgrade"){
        if (this.data.text_account && this.data.text_password) {
          console.log(1111)
          // console.log("ok two")
          wx.request({
            method: "POST",
            url: app.globalData.url + 'api/mine/guild/upgrade',
            data: {
              token: app.globalData.token, // token
              guild_id: this.data.guild_id, // 公会id
              platfrom_account: this.data.text_account,
              platfrom_password: this.data.text_password

            },
            success: function (e) {
              // console.log(e)
              if (e.data.code == "-1") {
                wx.showToast({
                  icon: "none",
                  title: e.data.message,
                })
              } else {
                wx.showModal({
                  content: '小主~您已成功提交审核，我们会在1-2个工作日进行审核，请及时查看审核进度~',
                  showCancel: false,
                  confirmColor: "#ddba69",
                  confirmText: "查看进度",
                  success: function () {
                    wx.redirectTo({
                      url: '../../../mine/guildManage/guildManage',
                    })
                  }
                })
              }
            }
          })
        } else {
          wx.showToast({
            icon: "none",
            title: '请先填写完整信息',
          })
        }
      } else if (this.data.type=="verification"){
        //验证
        if (this.data.text_account && this.data.text_password) {
          console.log(1111)
          // console.log("ok two")
          wx.request({
            method: "POST",
            url: app.globalData.url + 'api/mine/guild/again/auth',
            data: {
              token: app.globalData.token, // token
              guild_id: this.data.guild_id, // 公会id
              platfrom_account: this.data.text_account,
              platfrom_password: this.data.text_password

            },
            success: function (e) {
              // console.log(e)
              if (e.data.code == "-1") {
                wx.showToast({
                  icon: "none",
                  title: e.data.message,
                })
              } else {
                wx.showModal({
                  content: '小主~您已成功提交审核，我们会在1-2个工作日进行审核，请及时查看审核进度~',
                  showCancel: false,
                  confirmColor: "#ddba69",
                  confirmText: "查看进度",
                  success: function () {
                    wx.redirectTo({
                      url: '../../../mine/guildManage/guildManage',
                    })
                  }
                })
              }
            }
          })
        } else {
          wx.showToast({
            icon: "none",
            title: '请先填写完整信息',
          })
        }
      }else{
        if (this.data.text_guild && this.data.text_account && this.data.text_password) {
          // console.log("ok two")
          wx.request({
            method: "POST",
            url: app.globalData.url + 'api/mine/guild/add',
            data: {
              token: app.globalData.token, // token
              platfrom_name: this.data.p_name, // 平台名称
              guild_name: this.data.text_guild, // 公会名称
              platfrom_account: this.data.text_account,
              platfrom_password: this.data.text_password

            },
            success: function (e) {
              // console.log(e)
              if (e.data.code == "-1") {
                wx.showToast({
                  icon: "none",
                  title: e.data.message,
                })
              } else {
                wx.showModal({
                  content: '小主~您已成功提交审核，我们会在1-2个工作日进行审核，请及时查看审核进度~',
                  showCancel: false,
                  confirmColor: "#ddba69",
                  confirmText: "查看进度",
                  success: function () {
                    wx.redirectTo({
                      url: '../../../mine/guildManage/guildManage',
                    })
                  }
                })
              }
            }
          })
        } else {
          wx.showToast({
            icon: "none",
            title: '请先填写完整信息',
          })
        }
      }
      
    }else {
      wx.showToast({
        icon: "none",
        title: '请先阅读并勾选服务协议与保密协议',
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log("非bind")
    console.log(options)
    // if (options.verification){
    //   this.setData({
    //     verification:true,
    //   })
    // }
    if (options.p_id) {
      let p_id = (options.p_id).toString();
      let p_name = options.p_name;
      let p_table_flag = options.p_table_flag;
      this.setData({
        p_id: p_id, //选择平台后传回的平台id
        p_name: p_name,
        p_table_flag: p_table_flag,
      })
    } else if (options.type =="upgrade"){
      this.setData({
        guild_id: options.guild_id,
        guild_name:options.guild_name,
        platfrom_name: options.platfrom_name,
        type: "upgrade",
      })
    } else if (options.type == "verification"){
      this.setData({
        guild_id: options.guild_id,
        guild_name: options.guild_name,
        platfrom_name: options.platfrom_name,
        type: "verification",
      })
    }else{}
    // console.log(this.data.p_name)

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if(this.data.guild_id){
      this.setData({
        disabled:false,
      })
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})